package com.servlet;

import java.io.IOException;
import java.io.Writer;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.DBUtils.DBUtil;

/**
 * Servlet implementation class ShowReplyServlet
 */
@WebServlet("/ShowReplyServlet")
public class ShowReplyServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShowReplyServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    List<Integer> pidList=new ArrayList<>();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String idd=request.getParameter("id");
		int id=Integer.valueOf(idd);
		System.out.println("-->"+id);
		
		request.setCharacterEncoding("UTF-8");
		response.setContentType("UTF-8");
		response.setCharacterEncoding("UTF-8");
		Writer out=response.getWriter();
		Connection con=DBUtil.getconn();
		try {
			
			String sql="select replyId,reply,replyDate,replyNickName from bbsreply where FK_t="+id;
			Statement stm=con.createStatement();
			ResultSet rs=stm.executeQuery(sql);
			String sr="[";
			System.out.println(sql);
			while(rs.next()){
				System.out.println(rs.getInt(1));
				sr+="{\"id\":"+rs.getInt("replyId")+","+"\"txt\":\""+rs.getString("reply")+"\","+"\"replyDate\":\""+rs.getString("replyDate")+"\","+
				          "\"name\":\""+rs.getString("replyNickName")+"\"},";
			}
			String st=sr.substring(0, sr.length()-1);
			System.out.println(sr);
			st+="]";
			System.out.println(st);
			out.write(st);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
